package com.seio.edc.controller.system;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.nova.edc.model.system.*;
import com.nova.edc.service.system.DeviceTypeService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.nova.edc.service.system.CollectAreaItemService;
import com.nova.edc.service.system.DeviceLightService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.DateUtils;
import com.seio.edc.controller.core.BaseContoller;
import com.seio.edc.controller.system.customerinfo.CustomerinfoController;

@Controller
@RequestMapping(value = "/systemCollectArea")
public class CollectAreaContronller extends BaseContoller {

	static Logger log = Logger.getLogger(CustomerinfoController.class);

	@Autowired
	private com.nova.edc.service.system.CollectAreaService novaCollectAreaService;

	@Autowired
	private CollectAreaItemService novaCollectAreaItemService;

	@Autowired
	private DeviceLightService novaDeviceLightService;

	@Autowired
	private com.nova.edc.service.system.CustomerInfoService novaCustomerInfoService;

	@Autowired
	private DeviceTypeService deviceTypeService;

	private static final int PAGE_SIZE = 10;

	/**
	 * 用户表页面初始化
	 */
	@RequestMapping(value = "collectAreaList.shtml")
	public ModelAndView collectAreaList(HttpServletRequest request,
			HttpSession session, Integer pageIndex, CollectArea collectArea,
			ModelMap modelMap) {
		Login login = (Login) session.getAttribute("sessionLogin");
		collectArea.setAddby(login.getUsername());
		// 当前页数
		Page page = new Page();
		page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
		page.setPageSize(PAGE_SIZE);
		modelMap.put("pageIndex", page.getCurrentPage());
		novaCollectAreaService.trimCollectArea(collectArea);
		//获取区域的设备类型
		DeviceType deviceType = deviceTypeService.getByName(ModelConstant.DEVICE_LIGHTENERGY);
		collectArea.setDevicetype(deviceType.getId());
		PageList<CollectArea> pageList = novaCollectAreaService
				.findPageCollectArea(collectArea, page,
						"GatewayID,SensorID,ChannelID", "asc");
		modelMap.put("dataCount", page.getRowCount());
		modelMap.put("collectArea", collectArea);
		modelMap.put("pageCount", page.getTotalPage());
		// 数据列表
		modelMap.put("list", pageList.getDatas());

		return new ModelAndView("system/collectAreaList", modelMap);
	}

	/**
	 * 添加初始化
	 */
	@RequestMapping(value = "editCollectAreaInit.shtml")
	public ModelAndView editCollectAreaInit(HttpServletRequest request,
			HttpSession session, ModelMap modelMap, Integer collectAreaId) {
		Login login = (Login) session.getAttribute("sessionLogin");
		if (collectAreaId != null && collectAreaId != 0) {
			CollectArea collectArea = novaCollectAreaService
					.getById(collectAreaId);
			modelMap.put("collectArea", collectArea);
			List<CollectAreaItem> collectAreaItems = novaCollectAreaItemService
					.findByHeadId(collectArea.getId());
			modelMap.put("collectAreaItems", collectAreaItems);
		}

		// 获取所有的网关
		List<String> gateways = novaCollectAreaService.getAllGateWay();
		modelMap.put("gatewayInfo", JSON.toJSONString(gateways));

		// 所有客户
		modelMap.put("companyList", novaCustomerInfoService
				.findCustomerInfoByLogin(login.getUsername()));
		// 所有灯具
		List<DeviceLight> traLightList = novaDeviceLightService
				.findLightEnergy(ModelConstant.TRA_LIGHTTYPE);
		List<DeviceLight> ledLightList = novaDeviceLightService
				.findLightEnergy(ModelConstant.LED_LIGHTTYPE);
		if (traLightList != null && traLightList.size() > 0) {
			modelMap.put("traLightCodeInfo", JSON
					.toJSONString(novaDeviceLightService
							.getLightCodeByList(traLightList)));
		}
		if (ledLightList != null && ledLightList.size() > 0) {
			modelMap.put("ledLightCodeInfo", JSON
					.toJSONString(novaDeviceLightService
							.getLightCodeByList(ledLightList)));
		}

		return new ModelAndView("system/collectAreaEdit", modelMap);
	}

	/**
	 * 选择灯具
	 */
	@RequestMapping(value = "chooseLight.shtml")
	public ModelAndView chooseLight(HttpServletRequest request,
			HttpSession session, ModelMap modelMap, Integer pageIndex,
			DeviceLight lightEnergy) {
		// 所有灯具
		Page page = new Page();
		page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
		page.setPageSize(PAGE_SIZE);
		modelMap.put("pageIndex", page.getCurrentPage());
		PageList<DeviceLight> pageList = novaDeviceLightService
				.findPageLightEnergy(lightEnergy, page, "AddDate", "desc");
		modelMap.put("dataCount", page.getRowCount());
		modelMap.put("pageCount", page.getTotalPage());
		// 数据列表
		modelMap.put("list", pageList.getDatas());

		return new ModelAndView("system/chooseLight", modelMap);
	}

	/**
	 * 选择灯具
	 */
	@RequestMapping(value = "autoChooseLight.shtml")
	@ResponseBody
	public Map<String, Object> autoChooseLight(HttpServletRequest request,
			HttpSession session, ModelMap modelMap) {
		// 所有灯具
		List<DeviceLight> traLightList = novaDeviceLightService
				.findLightEnergy(ModelConstant.TRA_LIGHTTYPE);
		List<DeviceLight> ledLightList = novaDeviceLightService
				.findLightEnergy(ModelConstant.LED_LIGHTTYPE);
		modelMap.put("traLightCodes",
				novaDeviceLightService.getLightCodeByList(traLightList));
		modelMap.put("ledLightCodes",
				novaDeviceLightService.getLightCodeByList(ledLightList));
		return modelMap;
	}

	/**
	 * 添加collectArea
	 */
	@RequestMapping(value = "saveCollectArea.shtml")
	public ModelAndView saveCollectArea(CollectArea collectArea,
			BindingResult result, HttpServletRequest request,
			HttpSession session, ModelMap modelMap) {
		Login login = (Login) session.getAttribute("sessionLogin");
		if (collectArea.getId() != null && collectArea.getId() != 0) {
			collectArea.setModifyby(login.getUsername());
			collectArea.setModifydate(DateUtils.getCurrentDate());
			collectArea.setModifystamp(DateUtils.getCurrentDate());
			novaCollectAreaService.update(collectArea,
					collectArea.getCollectAreaItems());
		} else {
			if (collectArea.getCustomerinfoid() != null
					&& collectArea.getCustomerinfoid() != 0) {
				CustomerInfo customerInfo = novaCustomerInfoService
						.getById(collectArea.getCustomerinfoid());
				collectArea.setCompanycode(customerInfo.getCompanycode());
			}
            //设置添加采集区域的设备类型
            DeviceType deviceType = deviceTypeService.getByName(ModelConstant.DEVICE_LIGHTENERGY);
            collectArea.setDevicetype(deviceType.getId());
			collectArea.setAddby(login.getUsername());
			collectArea.setAdddate(DateUtils.getCurrentDate());
			novaCollectAreaService.insert(collectArea,
					collectArea.getCollectAreaItems());
		}
		return collectAreaList(request, session, 1, new CollectArea(), modelMap);
	}

	@RequestMapping(value = "deleteCollectArea.shtml")
	public ModelAndView deletecollectArea(Integer id,
			HttpServletRequest request, HttpSession session, ModelMap modelMap) {
		novaCollectAreaService.delete(id);
		return collectAreaList(request, session, 1, new CollectArea(), modelMap);
	}

}
